package com.voute.View.Activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.voute.App.App;
import com.voute.Base.BaseActivity;
import com.voute.Model.Bean.BaseBean;
import com.voute.Model.Bean.FeedCommentEntity;
import com.voute.Model.Bean.FeedDetailBean;
import com.voute.Model.Bean.VoteResultBean;
import com.voute.Presenter.IFeedDetailPresenter;
import com.voute.Presenter.Impl.FeedDetailPresenterImpl;
import com.voute.R;
import com.voute.Utils.DisplayUtil;
import com.voute.Utils.GlideUtils;
import com.voute.View.Adapter.FeedDetailCommentAdapter;
import com.voute.View.IView.IFeedDetailView;
import com.voute.Widget.ListViewForScrollView;

import java.util.ArrayList;
import java.util.List;


/**
 * feed详情画面
 * Created by liucxu on 2017/1/22.
 */

public class FeedDetailsActivity extends BaseActivity implements IFeedDetailView {

    private RelativeLayout star_layout;
    private RelativeLayout share_layout;
    private TextView feed_title_tv;//feed title
    private TextView feed_time_tv;
    private RelativeLayout before_vote_result_layout;
    private RelativeLayout vote_result_layout;
    private TextView ticket_num_tv;//一共的票数
    private RelativeLayout before_vote_right_layout;//投票之前右边部分，显示的内容是"去投票"
    private ImageView feed_detail_imv;
    private TextView textView2;//feed说明
    private CheckBox show_positive_radio;
    private CheckBox show_negative_radio;
    private ListViewForScrollView feed_attitude_list;
    private TextView vote_or_comment_tv;
    private RelativeLayout get_more_comments_layout;
    private TextView positive_num_tv;
    private TextView negative_num_tv;
    private ImageView feed_star_imv;
    private RelativeLayout positive_negative_layout;
    private LinearLayout close_dialog_layout;
    private RelativeLayout report_layout;
    private RelativeLayout feed_details_left_layout;
    private RelativeLayout feed_details_right_layout;
    private TextView tv_left_ticket;
    private TextView tv_right_ticket;

    /**
     * 变更UI之后新增加的样式
     */
    private ImageView imv_left_positive;

    private RelativeLayout layout_positive_layout;
    private ImageView imv_feed_detail_to_right;
    private ImageView left_line_imv;

    private RelativeLayout layout_both_layout;
    private ImageView both_line_imv;
    private ImageView imv_feed_detail_to_both;

    private RelativeLayout layout_right_layout;
    private ImageView right_line_imv;
    private ImageView imv_feed_detail_to_left;

    private ImageView imv_right_positive;

    private RelativeLayout back_layout;
    private MaterialDialog progressDialog;

    private IFeedDetailPresenter presenter;
    private FeedDetailCommentAdapter adapter;
    private FeedDetailBean bean;
    private View dialogVoteView;

    //    private MaterialDialog vote_dialog;
    private TextView click_me_yes;
    private TextView click_me_no;
    private ImageView count_imv;
    private TextView yes_count;
    private TextView no_count;

//    private Subscription rxSubscription;

    private int click_num = 0;
    private boolean clicked_positive = true;
    private boolean is_first_click = true;
    private CountDownTimer timer;
    String fid = "";

    private Dialog vote_dialog_self;
    private View vote_dialog_self_view;

    private Dialog share_dialog;
    private Dialog share_dialog_share;
    private View view_share_dialog;
    private View view_share_dialog_share;
    private TextView tv_positive_vote_num;
    private TextView tv_negative_vote_num;
    private LinearLayout wechat_circle_layout;
    private LinearLayout we_chat_layout;
    private LinearLayout weibo_layout;
    private LinearLayout close_dialog_layout_share;
    private RelativeLayout top_layout;
    private LinearLayout QQ_layout_share_after_vote;


    private LinearLayout wechat_circle_layout_share;
    private LinearLayout we_chat_layout_share;
    private LinearLayout weibo_layout_share;
    private LinearLayout QQ_layout_share;
    private LinearLayout close_dialog_layout_share_share;

    private MaterialDialog report_dialog;
    private View view_report_dialog;
    private EditText edt_report_reson;
    private TextView tv_close_dialog;
    private TextView tv_submit_report;

    private UMImage imageurl;

    private View.OnClickListener share_listener;

    private UMShareListener shareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onResult(SHARE_MEDIA share_media) {

        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {

        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {

        }
//        @Override
//        public void onResult(SHARE_MEDIA share_media) {
//            Toast.makeText(FeedDetailsActivity.this, "分享成功了", Toast.LENGTH_LONG).show();
//            SocializeUtils.safeCloseDialog(progressDialog);
//            if(share_dialog!=null){
//                if (share_dialog.isShowing()) {
//                    share_dialog.dismiss();
//                }
//            }
//            if(share_dialog_share!=null){
//                if (share_dialog_share.isShowing()) {
//                    share_dialog_share.dismiss();
//                }
//            }
//        }
//
//        @Override
//        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
//            SocializeUtils.safeCloseDialog(progressDialog);
//            Toast.makeText(FeedDetailsActivity.this, "分享失败" + throwable.getMessage(), Toast.LENGTH_LONG).show();
//            if(share_dialog!=null){
//                if (share_dialog.isShowing()) {
//                    share_dialog.dismiss();
//                }
//            }
//            if(share_dialog_share!=null){
//                if (share_dialog_share.isShowing()) {
//                    share_dialog_share.dismiss();
//                }
//            }
//        }
//
//        @Override
//        public void onCancel(SHARE_MEDIA share_media) {
//            SocializeUtils.safeCloseDialog(progressDialog);
//            Toast.makeText(FeedDetailsActivity.this, "分享取消了", Toast.LENGTH_LONG).show();
//            if(share_dialog!=null){
//                if (share_dialog.isShowing()) {
//                    share_dialog.dismiss();
//                }
//            }
//            if(share_dialog_share!=null){
//                if (share_dialog_share.isShowing()) {
//                    share_dialog_share.dismiss();
//                }
//            }
//        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.activity_feed_detail;
    }

    @Override
    public void setupView() {
        tv_right_ticket = $(R.id.tv_right_ticket);
        tv_left_ticket = $(R.id.tv_left_ticket);
        feed_details_left_layout = $(R.id.feed_details_left_layout);
        feed_details_right_layout = $(R.id.feed_details_right_layout);
        back_layout = $(R.id.back_layout);
        star_layout = $(R.id.star_layout);
        share_layout = $(R.id.share_layout);
        feed_title_tv = $(R.id.feed_title_tv);
        feed_time_tv = $(R.id.feed_time_tv);
        before_vote_result_layout = $(R.id.before_vote_result_layout);
        vote_result_layout = $(R.id.vote_result_layout);
        ticket_num_tv = $(R.id.ticket_num_tv);
        positive_num_tv = $(R.id.positive_num_tv);
        negative_num_tv = $(R.id.negative_num_tv);
        feed_star_imv = $(R.id.feed_star_imv);
        positive_negative_layout = $(R.id.positive_negative_layout);

        /**
         *
         */
        imv_left_positive = $(R.id.imv_left_positive);

        layout_positive_layout = $(R.id.layout_positive_layout);
        layout_positive_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                refreshDataUIBySelected(0);
            }
        });
        imv_feed_detail_to_right = $(R.id.imv_feed_detail_to_right);
        left_line_imv = $(R.id.left_line_imv);

        layout_both_layout = $(R.id.layout_both_layout);
        layout_both_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                refreshDataUIBySelected(1);
            }
        });
        both_line_imv = $(R.id.both_line_imv);
        imv_feed_detail_to_both = $(R.id.imv_feed_detail_to_both);

        layout_right_layout = $(R.id.layout_right_layout);
        layout_right_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                refreshDataUIBySelected(2);
            }
        });
        share_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bean.getData().isVoted()) {
                    if (share_dialog == null) {
                        initShareDialog();
                    }
                    share_dialog.show();
                    tv_positive_vote_num.setText(bean.getData().getPos_vote() + "");
                    tv_negative_vote_num.setText(bean.getData().getNeg_vote() + "");
                } else {
//                    MaterialDialog notice_dialog = new MaterialDialog.Builder(FeedDetailsActivity.this).title("提示").positiveText("确认").cancelable(false).content("请先投票才能分享").build();
//                    notice_dialog.show();
                    if (share_dialog_share == null) {
                        initBeforeVoteShareDialog();
                    }
                    share_dialog_share.show();
                }
            }
        });
        right_line_imv = $(R.id.right_line_imv);
        imv_feed_detail_to_left = $(R.id.imv_feed_detail_to_left);

        imv_right_positive = $(R.id.imv_right_positive);

        /**
         * 自定义投票Dialog
         */
        vote_dialog_self_view = View.inflate(FeedDetailsActivity.this, R.layout.dialog_vote_layout, null);

        /**
         * 分享Dialog
         */
        share_listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int id = view.getId();
                String share_url = "https://www.anyknew.com/feeds/" + bean.getData().getFid();
                imageurl = new UMImage(FeedDetailsActivity.this,bean.getData().getPic());
                imageurl.setThumb(new UMImage(FeedDetailsActivity.this, bean.getData().getPic()));
                imageurl.setTitle(bean.getData().getTitle());
                imageurl.setDescription(bean.getData().getTitle());
//                UMWeb web = new UMWeb(share_url);
//                web.setTitle(bean.getData().getTitle());
//                web.setThumb(new UMImage(FeedDetailsActivity.this, bean.getData().getPic()));
//                web.setDescription(bean.getData().getTitle());
                if (TextUtils.isEmpty(bean.getData().getDescription())) {
                    imageurl.setDescription(bean.getData().getTitle());
                } else {
                    imageurl.setDescription(bean.getData().getDescription());
                }
//                switch (id) {
//                    case R.id.wechat_circle_layout:
////                        new ShareAction(FeedDetailsActivity.this)
////                                .withText("欢迎使用5秒")
////                                .withMedia(imageurl)
////                                .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE.toSnsPlatform().mPlatform)
////                                .setCallback(shareListener).share();
////
//                        new ShareAction(FeedDetailsActivity.this)
//                                .withText("欢迎使用5秒")
//                                .withMedia(imageurl)
//                                .withTargetUrl(share_url)
//                                .withTitle(bean.getData().getTitle())
//                                .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE.toSnsPlatform().mPlatform)
//                                .setCallback(shareListener).share();
//                        break;
//                    case R.id.we_chat_layout:
////                        new ShareAction(FeedDetailsActivity.this)
////                                .withText("欢迎使用5秒")
////                                .withMedia(imageurl)
////                                .setPlatform(SHARE_MEDIA.WEIXIN.toSnsPlatform().mPlatform)
////                                .setCallback(shareListener).share();
//
//                        new ShareAction(FeedDetailsActivity.this)
//                                .withText("欢迎使用5秒")
//                                .withMedia(imageurl)
//                                .withTargetUrl(share_url)
//                                .withTitle(bean.getData().getTitle())
//                                .setPlatform(SHARE_MEDIA.WEIXIN.toSnsPlatform().mPlatform)
//                                .setCallback(shareListener).share();
//                        break;
//                    case R.id.weibo_layout:
////                        new ShareAction(FeedDetailsActivity.this)
////                                .withText("欢迎使用5秒")
////                                .withMedia(imageurl)
////                                .setPlatform(SHARE_MEDIA.SINA.toSnsPlatform().mPlatform)
////                                .setCallback(shareListener).share();
//
//                        new ShareAction(FeedDetailsActivity.this)
//                                .withText("欢迎使用5秒")
//                                .withMedia(imageurl)
//                                .withTargetUrl(share_url)
//                                .withTitle(bean.getData().getTitle())
//                                .setPlatform(SHARE_MEDIA.SINA.toSnsPlatform().mPlatform)
//                                .setCallback(shareListener).share();
//                        break;
//                    case R.id.wechat_circle_layout_share:
////                        new ShareAction(FeedDetailsActivity.this)
////                                .withText("欢迎使用5秒")
////                                .withMedia(imageurl)
////                                .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE.toSnsPlatform().mPlatform)
////                                .setCallback(shareListener).share();
//
//                        new ShareAction(FeedDetailsActivity.this)
//                                .withText("欢迎使用5秒")
//                                .withMedia(imageurl)
//                                .withTargetUrl(share_url)
//                                .withTitle(bean.getData().getTitle())
//                                .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE.toSnsPlatform().mPlatform)
//                                .setCallback(shareListener).share();
//                        break;
//                    case R.id.we_chat_layout_share:
////                        new ShareAction(FeedDetailsActivity.this)
////                                .withText("欢迎使用5秒")
////                                .withMedia(imageurl)
////                                .setPlatform(SHARE_MEDIA.WEIXIN.toSnsPlatform().mPlatform)
////                                .setCallback(shareListener).share();
//
//                        new ShareAction(FeedDetailsActivity.this)
//                                .withText("欢迎使用5秒")
//                                .withMedia(imageurl)
//                                .withTargetUrl(share_url)
//                                .withTitle(bean.getData().getTitle())
//                                .setPlatform(SHARE_MEDIA.WEIXIN.toSnsPlatform().mPlatform)
//                                .setCallback(shareListener).share();
//                        break;
//                    case R.id.weibo_layout_share:
////                        new ShareAction(FeedDetailsActivity.this)
////                                .withText("欢迎使用5秒")
////                                .withMedia(imageurl)
////                                .setPlatform(SHARE_MEDIA.SINA.toSnsPlatform().mPlatform)
////                                .setCallback(shareListener).share();
//
//                        new ShareAction(FeedDetailsActivity.this)
//                                .withText("欢迎使用5秒")
//                                .withMedia(imageurl)
//                                .withTargetUrl(share_url)
//                                .withTitle(bean.getData().getTitle())
//                                .setPlatform(SHARE_MEDIA.SINA.toSnsPlatform().mPlatform)
//                                .setCallback(shareListener).share();
//                        break;
//                    case R.id.QQ_layout_share:
////                        new ShareAction(FeedDetailsActivity.this)
////                                .withText("欢迎使用5秒")
////                                .withMedia(imageurl)
////                                .setPlatform(SHARE_MEDIA.QQ.toSnsPlatform().mPlatform)
////                                .setCallback(shareListener).share();
//
//                        new ShareAction(FeedDetailsActivity.this)
//                                .withText("欢迎使用5秒")
//                                .withMedia(imageurl)
//                                .withTargetUrl(share_url)
//                                .withTitle(bean.getData().getTitle())
//                                .setPlatform(SHARE_MEDIA.QQ.toSnsPlatform().mPlatform)
//                                .setCallback(shareListener).share();
//                        break;
//                    case R.id.QQ_layout_share_after_vote:
////                        new ShareAction(FeedDetailsActivity.this)
////                                .withText("欢迎使用5秒")
////                                .withMedia(imageurl)
////                                .setPlatform(SHARE_MEDIA.QQ.toSnsPlatform().mPlatform)
////                                .setCallback(shareListener).share();
//
//                        new ShareAction(FeedDetailsActivity.this)
//                                .withText("欢迎使用5秒")
//                                .withMedia(imageurl)
//                                .withTargetUrl(share_url)
//                                .withTitle(bean.getData().getTitle())
//                                .setPlatform(SHARE_MEDIA.QQ.toSnsPlatform().mPlatform)
//                                .setCallback(shareListener).share();
//                        break;
//                }
            }
        };
        view_share_dialog = View.inflate(FeedDetailsActivity.this, R.layout.dialog_share_layout, null);
        view_share_dialog_share = View.inflate(FeedDetailsActivity.this, R.layout.dialog_share_layout_share, null);
        tv_positive_vote_num = (TextView) view_share_dialog.findViewById(R.id.tv_positive_vote_num);
        tv_negative_vote_num = (TextView) view_share_dialog.findViewById(R.id.tv_negative_vote_num);
        wechat_circle_layout = (LinearLayout) view_share_dialog.findViewById(R.id.wechat_circle_layout);
        we_chat_layout = (LinearLayout) view_share_dialog.findViewById(R.id.we_chat_layout);
        weibo_layout = (LinearLayout) view_share_dialog.findViewById(R.id.weibo_layout);
        top_layout = (RelativeLayout) view_share_dialog.findViewById(R.id.top_layout);
        QQ_layout_share_after_vote=(LinearLayout)view_share_dialog.findViewById(R.id.QQ_layout_share_after_vote);
        close_dialog_layout_share = (LinearLayout) view_share_dialog.findViewById(R.id.close_dialog_layout_share);
        close_dialog_layout_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                share_dialog.dismiss();
            }
        });


        wechat_circle_layout_share = (LinearLayout) view_share_dialog_share.findViewById(R.id.wechat_circle_layout_share);
        we_chat_layout_share = (LinearLayout) view_share_dialog_share.findViewById(R.id.we_chat_layout_share);
        weibo_layout_share = (LinearLayout) view_share_dialog_share.findViewById(R.id.weibo_layout_share);
        QQ_layout_share = (LinearLayout) view_share_dialog_share.findViewById(R.id.QQ_layout_share);
        close_dialog_layout_share_share = (LinearLayout) view_share_dialog_share.findViewById(R.id.close_dialog_layout_share_share);
        close_dialog_layout_share_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                share_dialog_share.dismiss();
            }
        });


        QQ_layout_share_after_vote.setOnClickListener(share_listener);
        wechat_circle_layout.setOnClickListener(share_listener);
        we_chat_layout.setOnClickListener(share_listener);
        weibo_layout.setOnClickListener(share_listener);

        wechat_circle_layout_share.setOnClickListener(share_listener);
        we_chat_layout_share.setOnClickListener(share_listener);
        weibo_layout_share.setOnClickListener(share_listener);
        QQ_layout_share.setOnClickListener(share_listener);

        before_vote_right_layout = $(R.id.before_vote_right_layout);
        dialogVoteView = View.inflate(FeedDetailsActivity.this, R.layout.dialog_vote_layout, null);
        yes_count = (TextView) vote_dialog_self_view.findViewById(R.id.yes_count);
        no_count = (TextView) vote_dialog_self_view.findViewById(R.id.no_count);
        click_me_yes = (TextView) vote_dialog_self_view.findViewById(R.id.click_me_yes);
        click_me_no = (TextView) vote_dialog_self_view.findViewById(R.id.click_me_no);
        count_imv = (ImageView) vote_dialog_self_view.findViewById(R.id.count_imv);
        close_dialog_layout = (LinearLayout) vote_dialog_self_view.findViewById(R.id.close_dialog_layout);
        close_dialog_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (click_num != 0) {
                    initTimerCount();
                    if (clicked_positive) {
                        presenter.voteFeed(fid, click_num + "", "1");
                    } else {
                        presenter.voteFeed(fid, click_num + "", "0");
                    }
                }
                vote_dialog_self.dismiss();
            }
        });
        click_me_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                click_me_no.setEnabled(false);
                click_me_yes.setEnabled(true);
                clicked_positive = true;
                click_num = click_num + 1;
                yes_count.setTextColor(Color.parseColor("#efb836"));
                yes_count.setBackgroundResource(R.drawable.bg_vote_yes_count_selected);
                if (click_num < 10) {
                    yes_count.setText("00" + click_num);
                } else if ((click_num > 9) && (click_num < 100)) {
                    yes_count.setText("0" + click_num);
                } else {
                    yes_count.setText("" + click_num);
                }
                if (is_first_click) {
                    initTimerCount();
                    timer.start();
                    is_first_click = false;
                }
            }
        });
        click_me_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                click_me_no.setEnabled(true);
                click_me_yes.setEnabled(false);
                clicked_positive = false;
                click_num = click_num + 1;
                no_count.setTextColor(Color.parseColor("#02a18c"));
                no_count.setBackgroundResource(R.drawable.bg_vote_no_count_selected);
                if (click_num < 10) {
                    no_count.setText("00" + click_num);
                } else if ((click_num > 9) && (click_num < 100)) {
                    no_count.setText("0" + click_num);
                } else {
                    no_count.setText("" + click_num);
                }
                if (is_first_click) {
                    initTimerCount();
                    timer.start();
                    is_first_click = false;
                }
            }
        });
        before_vote_result_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checkLoginStatus()) {
                    yes_count.setText("000");
                    no_count.setText("000");
                    click_me_no.setEnabled(true);
                    click_me_yes.setEnabled(true);
                    click_num = 0;
                    clicked_positive = true;
                    is_first_click = true;
                    if (vote_dialog_self == null) {
                        initVoteDialog();
                    }
                    vote_dialog_self.show();
                }
            }
        });
        feed_detail_imv = $(R.id.feed_detail_imv);
        feed_detail_imv.setMaxHeight(9 * (DisplayUtil.GetDisplayWidth() - DisplayUtil.dip2px(App.GetInstance(), 24)) / 16);
        textView2 = $(R.id.textView2);
        show_positive_radio = $(R.id.show_positive_radio);
        show_negative_radio = $(R.id.show_negative_radio);
        feed_attitude_list = $(R.id.feed_attitude_list);
        feed_attitude_list.setFocusable(false);
        get_more_comments_layout = $(R.id.get_more_comments_layout);
        adapter = new FeedDetailCommentAdapter(this, new ArrayList<FeedCommentEntity>());
        star_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checkLoginStatus()) {
                    if (!bean.getData().isCollected()) {
                        presenter.starFeed(bean.getData().getFid() + "");
                    } else {
                        presenter.unStarFeed(bean.getData().getFid() + "");
                    }
                }
            }
        });
        back_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                App.GetInstance().finishActivity(FeedDetailsActivity.this);
            }
        });
        //查看更多评论
        get_more_comments_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checkLoginStatus()) {
                    if (!bean.getData().isVoted()) {
                        new MaterialDialog.Builder(FeedDetailsActivity.this).positiveText("确定").title("确定").content("查看更多评论前先投票").build().show();
                        return;
                    }
                    Intent intent = new Intent(FeedDetailsActivity.this, CommentsActivity.class);
                    intent.putExtra("key_num_comments", (bean.getData().getPos_comment() + bean.getData().getNeg_comment()) + "");
                    intent.putExtra("key_fid", bean.getData().getFid() + "");
                    if ("0".equals(bean.getData().getSide())) {
                        intent.putExtra("is_positive", false);
                    } else {
                        intent.putExtra("is_positive", true);
                    }
                    FeedDetailsActivity.this.startActivity(intent);
                }
            }
        });
        report_layout = $(R.id.report_layout);
        report_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (report_dialog == null) {
                    view_report_dialog = View.inflate(FeedDetailsActivity.this, R.layout.dialog_report, null);
                    edt_report_reson = (EditText) view_report_dialog.findViewById(R.id.edt_report_reson);
                    tv_close_dialog = (TextView) view_report_dialog.findViewById(R.id.tv_close_dialog);
                    tv_close_dialog.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            report_dialog.dismiss();
                        }
                    });
                    tv_submit_report = (TextView) view_report_dialog.findViewById(R.id.tv_submit_report);
                    tv_submit_report.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (TextUtils.isEmpty(edt_report_reson.getText().toString())) {
//                                Toast.makeText(FeedDetailsActivity.this, "请输入举报内容", Toast.LENGTH_SHORT).show();
                                MaterialDialog notice_dialog = new MaterialDialog.Builder(FeedDetailsActivity.this).title("提示").positiveText("确认").cancelable(false).content("请输入举报内容").build();
                                notice_dialog.show();
                            } else {
                                if (edt_report_reson.getText().toString().length() < 5 || edt_report_reson.getText().toString().length() > 100) {
//                                    Toast.makeText(FeedDetailsActivity.this, "输入举报内容的长度是5~100", Toast.LENGTH_SHORT).show();
                                    MaterialDialog notice_dialog = new MaterialDialog.Builder(FeedDetailsActivity.this).title("提示").positiveText("确认").cancelable(false).content("输入举报内容的长度是5~100").build();
                                    notice_dialog.show();
                                } else {
                                    presenter.reportFeed(bean.getData().getFid() + "", edt_report_reson.getText().toString());
                                    edt_report_reson.setText("");
                                }
                            }
                        }
                    });
                    report_dialog = new MaterialDialog.Builder(FeedDetailsActivity.this).customView(view_report_dialog, false).cancelable(false).build();
                }
                report_dialog.show();
            }
        });
        feed_attitude_list.setAdapter(adapter);
        setListViewHeightBasedOnChildren(feed_attitude_list);
        vote_or_comment_tv = $(R.id.vote_or_comment_tv);
        presenter = new FeedDetailPresenterImpl(this);

//        rxSubscription = RxBus.getDefault().toObservable(UpCommentAction.class).subscribe(new Action1<UpCommentAction>() {
//            @Override
//            public void call(UpCommentAction upCommentAction) {
//                if (checkLoginStatus()) {
//                    presenter.upComment(upCommentAction.getCid() + "", upCommentAction.getSide());
//                }
//            }
//        }, new Action1<Throwable>() {
//            @Override
//            public void call(Throwable throwable) {
//                Snackbar.make(feed_attitude_list, "咦，出错了", Snackbar.LENGTH_SHORT).show();
//            }
//        });
    }

    private void initVoteDialog() {
        vote_dialog_self = new Dialog(this, R.style.Theme_Light_Dialog);
        //获得dialog的window窗口
        Window window = vote_dialog_self.getWindow();
        //设置dialog在屏幕底部
        window.setGravity(Gravity.BOTTOM);
        window.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        android.view.WindowManager.LayoutParams lp = window.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        window.setAttributes(lp);
        //将自定义布局加载到dialog上
        vote_dialog_self.setCancelable(false);
        vote_dialog_self.setContentView(vote_dialog_self_view);
    }

    private void initShareDialog() {
        share_dialog = new Dialog(this, R.style.Theme_Light_Dialog);
        //获得dialog的window窗口
        Window window = share_dialog.getWindow();
        //设置dialog在屏幕底部
        window.setGravity(Gravity.BOTTOM);
        window.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        android.view.WindowManager.LayoutParams lp = window.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        window.setAttributes(lp);
        //将自定义布局加载到dialog上
        share_dialog.setCancelable(false);
        share_dialog.setContentView(view_share_dialog);
    }


    private void initBeforeVoteShareDialog() {
        share_dialog_share = new Dialog(this, R.style.Theme_Light_Dialog);
        //获得dialog的window窗口
        Window window = share_dialog_share.getWindow();
        //设置dialog在屏幕底部
        window.setGravity(Gravity.BOTTOM);
        window.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        android.view.WindowManager.LayoutParams lp = window.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        window.setAttributes(lp);
        //将自定义布局加载到dialog上
        share_dialog_share.setCancelable(false);
        share_dialog_share.setContentView(view_share_dialog_share);
    }

    private void resetPositiveNegativeLayout() {
        imv_left_positive.setImageResource(R.mipmap.feed_detail_left_positive_yellow);

        left_line_imv.setBackgroundResource(R.drawable.feed_detail_left_line);
        imv_feed_detail_to_right.setVisibility(View.GONE);

        both_line_imv.setBackgroundResource(R.drawable.feed_detail_both_line);
        imv_feed_detail_to_both.setVisibility(View.VISIBLE);

        right_line_imv.setBackgroundResource(R.drawable.feed_detail_right_line);
        imv_feed_detail_to_left.setVisibility(View.GONE);

        imv_right_positive.setImageResource(R.mipmap.feed_detail_right_negative_green);
    }

    private void refreshDataUIBySelected(final int position) {
        List<FeedCommentEntity> local_datas = new ArrayList<>();
        final ArrayList<FeedCommentEntity> selected_local_datas = new ArrayList<>();
        local_datas.addAll(bean.getData().getHot());

        if (position == 0) {
            for (FeedCommentEntity entity : local_datas) {
                if (entity.is_pos()) {
                    selected_local_datas.add(entity);
                }
            }
        } else if (position == 2) {
            for (FeedCommentEntity entity : local_datas) {
                if (!entity.is_pos()) {
                    selected_local_datas.add(entity);
                }
            }
        } else {
            selected_local_datas.addAll(local_datas);
        }
        adapter.setDatas(selected_local_datas);
        if (position == 0) {
            setSelectedPositive(true, false);
            setSelectedBoth(false);
            setSelectedNegative(false, true);
        } else if (position == 1) {
            setSelectedPositive(true, true);
            setSelectedBoth(true);
            setSelectedNegative(true, true);
        } else if (position == 2) {
            setSelectedPositive(false, true);
            setSelectedBoth(false);
            setSelectedNegative(true, false);
        }
    }

    private void setSelectedPositive(boolean selected, boolean hide_scroll) {
        if (selected) {
            if (hide_scroll) {
                imv_feed_detail_to_right.setVisibility(View.GONE);
            } else {
                imv_feed_detail_to_right.setVisibility(View.VISIBLE);
            }
            left_line_imv.setBackgroundResource(R.drawable.feed_detail_left_line);
            imv_feed_detail_to_right.setImageResource(R.mipmap.feed_detail_to_right);
            imv_left_positive.setImageResource(R.mipmap.feed_detail_left_positive_yellow);
        } else {
            imv_feed_detail_to_right.setVisibility(View.GONE);
            left_line_imv.setBackgroundResource(R.drawable.feed_detail_left_line_unselected);
            imv_left_positive.setImageResource(R.mipmap.feed_detail_left_positive_grey);
        }
    }

    private void setSelectedBoth(boolean selected) {
        if (selected) {
            imv_feed_detail_to_both.setVisibility(View.VISIBLE);
        } else {
            imv_feed_detail_to_both.setVisibility(View.GONE);
        }
    }

    private void setSelectedNegative(boolean selected, boolean hide_scroll) {
        if (selected) {
            if (hide_scroll) {
                imv_feed_detail_to_left.setVisibility(View.GONE);
            } else {
                imv_feed_detail_to_left.setVisibility(View.VISIBLE);
            }
            right_line_imv.setBackgroundResource(R.drawable.feed_detail_right_line);
            imv_right_positive.setImageResource(R.mipmap.feed_detail_right_negative_green);
            imv_feed_detail_to_left.setImageResource(R.mipmap.feed_detail_to_left);
        } else {
            imv_feed_detail_to_left.setVisibility(View.GONE);
            right_line_imv.setBackgroundResource(R.drawable.feed_detail_right_line_unselected);
            imv_right_positive.setImageResource(R.mipmap.feed_detail_right_negative_grey);
        }
    }

    @Override
    public void setupData() {
        Intent intent = getIntent();
        fid = intent.getStringExtra("key_fid");
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (TextUtils.isEmpty(fid)) {
            this.finish();
        } else {
            presenter.getFeedDetails(fid);
        }
    }

    @Override
    public void showFeedDetail(FeedDetailBean detail) {
        if (detail != null) {
            bean = detail;
            feed_title_tv.setText(bean.getData().getTitle());
            feed_time_tv.setText(bean.getData().getAdd_date());
            GlideUtils.getInstance().loadSimpleContextVitMap(this, bean.getData().getPic(), feed_detail_imv);
            if (TextUtils.isEmpty(bean.getData().getDescription())) {
                textView2.setVisibility(View.GONE);
            } else {
                textView2.setVisibility(View.VISIBLE);
                textView2.setText(bean.getData().getDescription());
            }
            //判断是否投过票
            if (bean.getData().isVoted()) {
                before_vote_result_layout.setVisibility(View.GONE);
                vote_result_layout.setVisibility(View.VISIBLE);
                positive_num_tv.setText(detail.getData().getPos_vote() + "");
                negative_num_tv.setText(detail.getData().getNeg_vote() + "");
                vote_or_comment_tv.setText("立即评论");
                //反方
                if (bean.getData().getSide() == 0) {
                    feed_details_left_layout.setBackgroundResource(R.drawable.bg_feed_detail_positive_unvote);
                    feed_details_right_layout.setBackgroundResource(R.drawable.bg_feed_detail_negative_select);
                    positive_num_tv.setTextColor(Color.parseColor("#666666"));
                    tv_left_ticket.setTextColor(Color.parseColor("#666666"));
                    negative_num_tv.setTextColor(Color.parseColor("#000000"));
                    tv_right_ticket.setTextColor(Color.parseColor("#000000"));
                }
                //正方
                else if (bean.getData().getSide() == 1) {
                    feed_details_left_layout.setBackgroundResource(R.drawable.bg_feed_detail_positive);
                    feed_details_right_layout.setBackgroundResource(R.drawable.bg_feed_detail_negative);
                    positive_num_tv.setTextColor(Color.parseColor("#000000"));
                    tv_left_ticket.setTextColor(Color.parseColor("#000000"));
                    negative_num_tv.setTextColor(Color.parseColor("#666666"));
                    tv_right_ticket.setTextColor(Color.parseColor("#666666"));
                }
            } else {
                ticket_num_tv.setText("已有" + bean.getData().getAll_vote() + "票，投票后见结果");
                before_vote_result_layout.setVisibility(View.VISIBLE);
                vote_result_layout.setVisibility(View.GONE);
                vote_or_comment_tv.setText("立即投票");
            }
            if (detail.getData().isCollected()) {
                feed_star_imv.setImageResource(R.mipmap.feed_detail_stared);
            } else {
                feed_star_imv.setImageResource(R.mipmap.fed_detail_star);
            }
            vote_or_comment_tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (checkLoginStatus()) {
                        if (bean.getData().isVoted()) {
                            Intent intent = new Intent(FeedDetailsActivity.this, PostCommentsActivity.class);
                            intent.putExtra("key_fid", bean.getData().getFid() + "");
                            if ("0".equals(bean.getData().getSide())) {
                                intent.putExtra("is_positive", false);
                            } else {
                                intent.putExtra("is_positive", true);
                            }
                            FeedDetailsActivity.this.startActivity(intent);
                        } else {
                            if (checkLoginStatus()) {
                                yes_count.setText("000");
                                no_count.setText("000");
                                click_me_no.setEnabled(true);
                                click_me_yes.setEnabled(true);
                                click_num = 0;
                                clicked_positive = true;
                                is_first_click = true;
                                if (vote_dialog_self == null) {
                                    initVoteDialog();
                                }
                                vote_dialog_self.show();
                            }
                        }
                    }
                }
            });
            ArrayList<FeedCommentEntity> entities = new ArrayList<FeedCommentEntity>();
//            entities.addAll(detail.getData().getComments().getPos());
//            entities.addAll(detail.getData().getComments().getNeg());
            entities.addAll(detail.getData().getHot());
            if (entities.size() > 0) {
//                get_more_comments_layout.setVisibility(View.VISIBLE);
                positive_negative_layout.setVisibility(View.VISIBLE);
                resetPositiveNegativeLayout();
            } else {
//                get_more_comments_layout.setVisibility(View.GONE);
                positive_negative_layout.setVisibility(View.GONE);
            }
            if (detail.getData().getPos_comment() + detail.getData().getNeg_comment() > 0) {
                get_more_comments_layout.setVisibility(View.VISIBLE);
            } else {
                get_more_comments_layout.setVisibility(View.GONE);
            }
            adapter.setDatas(entities);
            setListViewHeightBasedOnChildren(feed_attitude_list);
        } else {
            MaterialDialog notice_dialog = new MaterialDialog.Builder(FeedDetailsActivity.this).title("提示").positiveText("确认").cancelable(false).content("数据载入错误").build();
            notice_dialog.show();
        }
    }

    @Override
    public void showVoteStatus(boolean voted, int positive_num, int negative_num) {

    }

    @Override
    public void showPositiveComment(boolean positive, boolean negative) {

    }

    @Override
    public void doComment() {

    }

    @Override
    public void starSuccess() {
        feed_star_imv.setImageResource(R.mipmap.feed_detail_stared);
        bean.getData().setCollected(true);
//        Snackbar.make(star_layout, "收藏成功", Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public void voteSuccess(VoteResultBean result) {
        presenter.getFeedDetails(fid);
        if (share_dialog == null) {
            initShareDialog();
        }
//        share_dialog.show();
//        tv_positive_vote_num.setText(result.getData().getPos_vote() + "");
//        tv_negative_vote_num.setText(result.getData().getNeg_vote() + "");
    }

    @Override
    public void unstarSuccess() {
        feed_star_imv.setImageResource(R.mipmap.fed_detail_star);
        bean.getData().setCollected(false);
//        Snackbar.make(star_layout, "取消收藏成功", Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public void upCommentSuccess() {
        presenter.getFeedDetails(fid);
    }

    @Override
    public void upCommentFailure(BaseBean response) {
//        Snackbar.make(feed_star_imv, response.getMsg(), Snackbar.LENGTH_SHORT).show();
        MaterialDialog notice_dialog = new MaterialDialog.Builder(FeedDetailsActivity.this).title("提示").positiveText("确认").cancelable(false).content(response.getMsg()).build();
        notice_dialog.show();
    }

    @Override
    public void reportSuccess() {
        report_dialog.dismiss();
//        Snackbar.make(feed_star_imv, "举报话题成功", Snackbar.LENGTH_SHORT).show();
        MaterialDialog notice_dialog = new MaterialDialog.Builder(FeedDetailsActivity.this).title("提示").positiveText("确认").cancelable(false).content("举报话题成功").build();
        notice_dialog.show();
    }

    @Override
    public void reportFailure(String response) {
        report_dialog.dismiss();
//        Snackbar.make(feed_star_imv, response, Snackbar.LENGTH_SHORT).show();
        MaterialDialog notice_dialog = new MaterialDialog.Builder(FeedDetailsActivity.this).title("提示").positiveText("确认").cancelable(false).content(response).build();
        notice_dialog.show();
    }

    @Override
    public void showLoading() {
        if (progressDialog == null) {
            progressDialog = new MaterialDialog.Builder(this)
                    .progress(true, 0)
                    .title("提示")
                    .content("请等待...")
                    .autoDismiss(false)
                    .cancelable(true)
                    .build();
        }
        progressDialog.show();
    }

    @Override
    public void hideLoading() {
        progressDialog.dismiss();
    }

    @Override
    public void showError(String errMsg) {
//        Snackbar.make(back_layout, errMsg, Snackbar.LENGTH_SHORT).show();
        MaterialDialog notice_dialog = new MaterialDialog.Builder(FeedDetailsActivity.this).title("提示").positiveText("确认").cancelable(false).content(errMsg).build();
        notice_dialog.show();
    }

    public void setListViewHeightBasedOnChildren(ListView listView) {
    }

    private void initTimerCount() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        timer = new CountDownTimer(5000, 1000) {
            @Override
            public void onTick(long l) {
                int remain = (int) l / 1000;
                switch (remain) {
                    case 5:
                        count_imv.setImageResource(R.mipmap.icon_djs5);
                        break;
                    case 4:
                        count_imv.setImageResource(R.mipmap.icon_djs4);
                        break;
                    case 3:
                        count_imv.setImageResource(R.mipmap.icon_djs3);
                        break;
                    case 2:
                        count_imv.setImageResource(R.mipmap.icon_djs2);
                        break;
                    case 1:
                        count_imv.setImageResource(R.mipmap.icon_djs1);
                        break;
                    case 0:
                        break;
                }
            }

            @Override
            public void onFinish() {
                count_imv.setImageResource(R.mipmap.icon_djs0);
                vote_dialog_self.dismiss();
                if (clicked_positive) {
                    presenter.voteFeed(fid, click_num + "", "1");
                } else {
                    presenter.voteFeed(fid, click_num + "", "0");
                }
                click_num = 0;
            }
        };
    }

    @Override
    protected void onDestroy() {
//        if (!rxSubscription.isUnsubscribed()) {
//            rxSubscription.unsubscribe();
//        }
        UMShareAPI.get(this).release();
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}
